package com.dcbeida.activity.addnewpatient.historyofsmoking;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dcbeida.R;
import com.dcbeida.activity.BaseActivity;
import com.dcbeida.bean.HistoryOfSmokingBean;
import com.dcbeida.bean.Tag;
import com.dcbeida.utils.Utils;
import com.dcbeida.widget.FlowLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 病理分型
 * Created by 150410 on 2017/9/7.
 */

public class TypeAct extends BaseActivity implements HistoryOfSmokingContract.View {
    @Bind(R.id.img_back)
    ImageView imgBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_right)
    TextView tvRight;
    @Bind(R.id.rel_title1)
    RelativeLayout relTitle1;
    @Bind(R.id.fl_type)
    FlowLayout flType;
    @Bind(R.id.et_other)
    EditText etOther;
    @Bind(R.id.ll_other)
    LinearLayout llOther;
    private ArrayList<String> typeList;

    private List<Tag> typeTags = new ArrayList<>();
    private List<Tag> typeSelectedTags = new ArrayList<>();
    private List<Tag> yyxsSelectedTags = new ArrayList<>();
    private HistoryOfSmokingPresenter presenter;
    private String otherString = "";
    private String parentid = "";//5:病理分析 12:用药线数
    private String yyxsOtherString = "";

    @Override
    protected int getLayoutResId() {
        return R.layout.type_act;
    }

    @Override
    public void setData() {
        super.setData();
        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            String title = bundle.getString("title");
            parentid = bundle.getString("parentid");
            otherString = bundle.getString("otherString");
            yyxsOtherString = bundle.getString("yyxsOtherString");
            if ("5".equals(parentid)) {
                typeSelectedTags = bundle.getParcelableArrayList("typeSelectedTags");
            } else {
                yyxsSelectedTags = bundle.getParcelableArrayList("yyxsSelectedTags");
            }
            tvTitle.setText(title);

        }
        tvRight.setText(R.string.bc);
        imgBack.setVisibility(View.VISIBLE);
        tvRight.setVisibility(View.VISIBLE);
        tvTitle.setVisibility(View.VISIBLE);
        flType.setSpec(Utils.dip2px(TypeAct.this, 8), Utils.dip2px(TypeAct.this, 10));
        typeList = new ArrayList<>();

        presenter = new HistoryOfSmokingPresenter(this, this);
        presenter.getData(parentid, "0", "0");
    }

    private void updataTags(List<Tag> tags, FlowLayout flowLayout, final List<Tag> selectedTags) {
        for (int i = 0; i < tags.size(); i++) {
            addTas(tags.get(i), flowLayout, selectedTags);
        }
    }

    private void addTas(final Tag tag, FlowLayout flowLayout, final List<Tag> selectedTags) {
        final View view = LayoutInflater.from(this).inflate(R.layout.tag_item, null);
        final TextView tvTagName = (TextView) view.findViewById(R.id.tvTagName);

        tvTagName.setText(tag.getLableDesc());
        tvTagName.setTextAppearance(this, R.style.TextAppear_Theme_c_444444_Size15);
        view.setBackgroundResource(R.drawable.shape_rectangle_c_e5);

        if (selectedTags.contains(tag)) {
            tvTagName.setTextAppearance(this, R.style.TextAppear_Theme_ffffff_Size15);
            view.setBackgroundResource(R.drawable.shape_rectangle_c_0c87ea);
        } else {
            tvTagName.setTextAppearance(this, R.style.TextAppear_Theme_c_444444_Size15);
            view.setBackgroundResource(R.drawable.shape_rectangle_c_e5);
        }

        if (selectedTags.size() > 0) {
            for (Tag obj : selectedTags) {
                if (obj.getOrgId().equals("99")) {
                    llOther.setVisibility(View.VISIBLE);
                    if ("5".equals(parentid)) {
                        etOther.setText(otherString);
                    } else {
                        etOther.setText(yyxsOtherString);
                    }
                    break;
                } else {
                    llOther.setVisibility(View.GONE);
                    etOther.setText("");
                    etOther.setText("");
                }
            }
        } else {
            llOther.setVisibility(View.GONE);
            etOther.setText("");
        }

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (selectedTags.contains(tag)) {
                    selectedTags.remove(tag);
                    tvTagName.setTextAppearance(TypeAct.this, R.style.TextAppear_Theme_c_444444_Size15);
                    view.setBackgroundResource(R.drawable.shape_rectangle_c_e5);
                } else {
                    selectedTags.add(tag);
                    tvTagName.setTextAppearance(TypeAct.this, R.style.TextAppear_Theme_ffffff_Size15);
                    view.setBackgroundResource(R.drawable.shape_rectangle_c_0c87ea);

                }
                if (selectedTags.size() > 0) {
                    for (Tag tag : selectedTags) {
                        if (tag.getOrgId().equals("99")) {
                            llOther.setVisibility(View.VISIBLE);
                            etOther.setText(etOther.getText().toString());
                            break;
                        } else {
                            llOther.setVisibility(View.GONE);
                        }
                    }
                } else {
                    llOther.setVisibility(View.GONE);
                    etOther.setText("");
                }

            }
        });


        view.setTag(tag);
        flowLayout.addView(view);
    }


    @OnClick({R.id.img_back, R.id.tv_right})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.tv_right:

                Intent intent = new Intent();
                Bundle bundle = new Bundle();

                if ("5".equals(parentid)) {
                    for (Tag tag : typeSelectedTags) {
                        if (tag.getOrgId().equals("99")) {
                            tag.setOther(etOther.getText().toString());
                            break;
                        }
                    }
                    if (!TextUtils.isEmpty(etOther.getText().toString())) {
                        bundle.putString("otherString", etOther.getText().toString());
                    }
                    bundle.putParcelableArrayList("typeSelectedTags", (ArrayList<? extends Parcelable>) typeSelectedTags);
                } else {
                    for (Tag tag : yyxsSelectedTags) {
                        if (tag.getOrgId().equals("99")) {
                            tag.setOther(etOther.getText().toString());
                            break;
                        }
                    }
                    if (!TextUtils.isEmpty(etOther.getText().toString())) {
                        bundle.putString("yyxsOtherString", etOther.getText().toString());
                    }
                    bundle.putParcelableArrayList("yyxsSelectedTags", (ArrayList<? extends Parcelable>) yyxsSelectedTags);
                }

                intent.putExtras(bundle);
                setResult(RESULT_OK, intent);
                finish();
                break;
        }
    }

    @Override
    public void setPresenter(HistoryOfSmokingContract.Present presenter) {

    }

    @Override
    public void getDataError(String msg, int type) {
        showTip(msg);
    }

    @Override
    public void getDataSuccess(HistoryOfSmokingBean historyOfSmokingBean) {
        ArrayList<HistoryOfSmokingBean.ListBean> listBeen = historyOfSmokingBean.getList();
        for (int i = 0; i < listBeen.size(); i++) {
            typeTags.add(new Tag(listBeen.get(i).getName(), listBeen.get(i).getId() + "", listBeen.get(i).getValue() + "", ""));
        }
        if ("5".equals(parentid)) {
            updataTags(typeTags, flType, typeSelectedTags);
        } else {
            updataTags(typeTags, flType, yyxsSelectedTags);
        }
    }

    @Override
    public void showTip(String msg) {
        super.showTip(msg);
    }
}
